// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * System_Int32.cpp
 *
 *  Created on: Jan 8, 2010
 *      Author: dhovemey
 */

#include <cassert>
#include <cstdint>
#include <string>
#include "util/String.h"
#include "interp/VM_Interpreter.h"
#include "interp/VM_Object.h"
#include "interp/VM_MagicMethod.h"

extern "C"
VM_Object* System_Int32_toString(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	std::wstring str = String::asWString(VM_RECEIVER->getMagicInt());
	return VM_INTERP->getVMString(str, thread);
}

extern "C"
VM_Object* System_Int32_equals(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2 || !VM_INTERP->isType(VM_METHOD_ARG(1), CLASS_ID_INT32)) {
		// TODO: throw exception?
		return VM_INTERP->getVMBoolean(false);
	}

	return VM_INTERP->getVMBoolean(VM_RECEIVER->getMagicInt() == VM_METHOD_ARG(1)->getMagicInt());
}

extern "C"
VM_Object* System_Int32_compareTo(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2) {
		// TOOD: throw exception?
		return VM_INTERP->getNil();
	}

	int32_t cmp;
	if (!VM_INTERP->isType(VM_METHOD_ARG(1), CLASS_ID_INT32)) {
		cmp = VM_INTERP->compareDifferentTypes(VM_RECEIVER, VM_METHOD_ARG(1));
	} else {
		int32_t lhs = VM_RECEIVER->getMagicInt();
		int32_t rhs = VM_METHOD_ARG(1)->getMagicInt();

		if (lhs < rhs) {
			cmp = -1;
		} else if (lhs > rhs) {
			cmp = 1;
		} else {
			cmp = 0;
		}
	}

	return VM_INTERP->getVMInt32(cmp, thread);
}

// +
extern "C"
VM_Object* System_Int32_2B(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2 || !VM_INTERP->isType(VM_METHOD_ARG(1), CLASS_ID_INT32)) {
		// TODO: throw exception?
		printf("numArgs = %u\n", numArgs);
		return VM_INTERP->getNil();
	}

	int32_t sum = VM_RECEIVER->getMagicInt() + VM_METHOD_ARG(1)->getMagicInt();
	return VM_INTERP->getVMInt32(sum, thread);
}

// -
extern "C"
VM_Object* System_Int32_2D(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2 || !VM_INTERP->isType(VM_METHOD_ARG(1), CLASS_ID_INT32)) {
		// TODO: throw exception?
		printf("numArgs = %u\n", numArgs);
		return VM_INTERP->getNil();
	}

	int32_t diff = VM_RECEIVER->getMagicInt() - VM_METHOD_ARG(1)->getMagicInt();
	return VM_INTERP->getVMInt32(diff, thread);
}
